package com.xiaoli.study.controller;

import com.netflix.discovery.EurekaClient;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>Description: []</p>
 * Copyright (c) 2017 北京柯莱特科技有限公司
 * Created on 2018/5/17
 *
 * @author <a href="mailto: liruifeng@camelotchina.com">李瑞丰</a>
 * @version 1.0
 */
@RestController
public class HelloWorldController {

    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping(value = "sayHello", method = RequestMethod.GET)
    public String sayHello(String name) {
        ServiceInstance localServiceInstance = discoveryClient.getLocalServiceInstance();
        if (StringUtils.isEmpty(name)) {
            return "who are you?";
        }
        return String.format("hello %s! my host is %s and my service_id is %s", name, localServiceInstance.getHost(), localServiceInstance.getServiceId());
    }

}
